﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.FoodTesting.WaterFood
{
    public partial class WaterEdit : System.Web.UI.Page
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_WaterData tWaterData = new T_WaterData();              //Vc检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["FoodTime"] != null && Request.QueryString["FoodNum"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        public void LoadData()
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_WaterData md_WaterData = new E_WaterData();
            md_WaterData.WaterTime = date;
            md_WaterData.RawId = RawId;
            List<E_WaterData> eVcDataList = tWaterData.GetDataListByTime(md_WaterData);

            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 2;//水分含量
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eFoodRecord != null)
            {
                this.labMean.Text = eFoodRecord.FoodResult;
            }

            if (eVcDataList != null)
            {
                int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
                E_WaterData eWaterData = eVcDataList.Where(p => p.FoodNum == FoodNum).FirstOrDefault();//获取第一次检测记录
                if (eWaterData != null)
                {
                    //this.txt_buwei.Text = eWaterData.RawPosition;
                    //this.txt_m0.Text = eWaterData.Water_m0.ToString();
                    this.txt_m1.Text = eWaterData.Water_m1.ToString();
                    this.txt_m2.Text = eWaterData.Water_m2.ToString();
                    this.txt_m1_s.Text = eWaterData.Water_m1_S.ToString(); //当作m3 使用
                    //this.txt_m2_s.Text = eWaterData.Water_m2_S.ToString();
                    this.txt_content.Text = eWaterData.WaterContent.ToString();
                }
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawId = Convert.ToInt32(Request.QueryString["RawID"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"]);
            int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
            try
            {
                //string buwei = this.txt_buwei.Text.Trim();
                //double m0 = Convert.ToDouble(this.txt_m0.Text.Trim());
                double m1 = Convert.ToDouble(this.txt_m1.Text.Trim());
                double m2 = Convert.ToDouble(this.txt_m2.Text.Trim());
                double m1_s = Convert.ToDouble(this.txt_m1_s.Text.Trim());
                //double m2_s = Convert.ToDouble(this.txt_m2_s.Text.Trim());
                double content = (m1 - m2) / (m1 - m1_s) * 100;// Convert.ToDouble(this.txt_content.Text.Trim());

                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                E_WaterData eWaterData = new E_WaterData();
                eWaterData.RawId = RawId;
                eWaterData.RawName = eRawMaterial.RAWName;
                eWaterData.WaterContent = content.ToString();
                eWaterData.RawPosition = "";
                eWaterData.Water_m0 = "";
                eWaterData.Water_m1 = m1.ToString();
                eWaterData.Water_m2 = m2.ToString();
                eWaterData.Water_m1_S = m1_s.ToString();
                eWaterData.Water_m2_S = "";
                eWaterData.WaterTime = date;
                eWaterData.FoodNum = FoodNum;
                tWaterData.RemoveByFoodNum(eWaterData);   //删除现有的检测记录
                tWaterData.Add(eWaterData);               //添加检测记录

                this.txt_content.Text = content.ToString();
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('输入有误！');</script>");
            }
        }

        //计算平均值
        protected void btnMean_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_WaterData md_WaterData = new E_WaterData();
            md_WaterData.WaterTime = date;
            md_WaterData.RawId = RawId;
            List<E_WaterData> eVcDataList = tWaterData.GetDataListByTime(md_WaterData);


            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 2;//Vc检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eVcDataList == null || eVcDataList.Count < 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先提交检测数据！');</script>");
                return;
            }

            double Result = 0;//检测结果
            eVcDataList.ForEach(p =>
            {
                Result += Convert.ToDouble(p.WaterContent);
            });

            if (eFoodRecord != null)  //若存在则进行修改
            {
                eFoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                tFoodRecord.Update(eFoodRecord);
                this.labMean.Text = eFoodRecord.FoodResult;
            }
            else
            {
                md_FoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                md_FoodRecord.RawName = eRawMaterial.RAWName;
                tFoodRecord.Add(md_FoodRecord);
                this.labMean.Text = md_FoodRecord.FoodResult;
            }

            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('计算完毕！');parent.window.RefValue();</script>");
        }
    }
}